package com.example.order.service.impl;

import com.example.order.entity.OrderEntity;
import com.example.order.entity.OrderStatus;
import com.example.order.repository.OrderRepository;
import com.example.order.service.ShopOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Random;

/**
 * @author : zhayh
 * @date : 2021-5-8 15:44
 * @description :
 */

@Slf4j
@Service
public class ShopOrderServiceImpl implements ShopOrderService {
    @Resource
    private OrderRepository orderRepository;
    @Resource
    private OrderQueueService orderQueueService;

    @Override
    public OrderEntity confirmOrder(String orderCode) {
        OrderEntity orderEntity = orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            orderEntity.setOrderStatus(OrderStatus.MAKING.getName());
            orderEntity.setOrderConfirmTime(LocalDateTime.now());
            orderEntity.setOrderShopName("第1门店");
            orderRepository.save(orderEntity);
            log.info("商家数据服务，订单已确认： {}", orderCode);

            return processOrder(orderCode);
        }
        return null;
    }

    @Override
    public OrderEntity processOrder(String orderCode) {
        OrderEntity orderEntity = orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            log.info("商家数据服务，订单开始制作： {}", orderCode);
            Random random = new Random();
            int delay = random.nextInt(30) + 10;
            // 模拟制作
            try {
                Thread.sleep(delay * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            orderEntity.setOrderStatus(OrderStatus.WAITINGRIDER.getName());
            orderEntity.setOrderMadeTime(LocalDateTime.now());
            orderEntity.setOrderShopName("第1门店");
            orderEntity = orderRepository.save(orderEntity);
            log.info("商家数据服务，订单制作完成，进入配送状态： {}", orderCode);

            // 向队列2发送待配送消息
            orderQueueService.sendOrderDeliverMessage(orderEntity.getOrderCode());
            return orderEntity;
        }
        return null;
    }

    @Override
    public OrderEntity finishDeliver(String orderCode) {
        OrderEntity orderEntity = orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            log.info("商家数据服务，订单开始配送： {}", orderCode);
            Random random = new Random();
            int delay = random.nextInt(30) + 10;
            // 模拟制作
            try {
                Thread.sleep(delay * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            orderEntity.setOrderDeliverName("商家配送");
            orderEntity.setOrderStatus(OrderStatus.DELIVEFINISHED.getName());
            orderEntity.setOrderCompleteTime(LocalDateTime.now());
            orderEntity = orderRepository.save(orderEntity);
            log.info("商家数据服务，商家配送完成： {}", orderCode);

            // 向队列4发生商家配送完成消息
            orderQueueService.sendCompleteMessage(orderEntity.getOrderCode());
            return orderEntity;
        }
        return null;
    }
    
    // 商家监听队列1，实现商家自动接单
    @RabbitListener(queues = "queue.order.confirm")
    public void listenOrderConfirm(String message) {
        log.info("商家数据服务，收到待确认订单： {}", message);
        OrderEntity orderEntity = confirmOrder(message);
    }

    // 商家监听队列3， 实现商家自动配送
    @RabbitListener(queues = "queue.shop.deliver")
    public void listenShopDeliver(String message){
        log.info("商家数据服务，收到无骑手配送订单，由商家安排接单： {}", message);
        OrderEntity orderEntity = finishDeliver(message);
    }
}
